package karaokestrong.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import karaokestrong.excepcion.DAOExcepcion;
import karaokestrong.modelo.Comprobante;
import karaokestrong.util.ConexionBD;

public class ComprobanteDAO extends BaseDAO {

	public int insertarComprobante(Comprobante c) throws DAOExcepcion{
		System.out.println("ProuctoDAO: insertar(Producto vo)");
		String query = "INSERT INTO comprobante (fecha, monto, idcajero) VALUES (now(), 100, 1);";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		int id = 0;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			/*stmt.setString(1, p.getTipo());
			stmt.setInt(2, p.getCantidad());
			stmt.setTimestamp(3, p.getFecha());
			stmt.setInt(4, p.getCliente().getIdcliente());
			stmt.setInt(5, p.getComprobante().getId());*/
			System.out.println("Registro Exitoso");
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
			
			// Obtener el ultimo id
			
			query = "SELECT LAST_INSERT_ID()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				id = rs.getInt(1);
			}
			
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return id;
		
	}
}
